//
//  ZYSTableVCHeader.m
//  budejie
//
//  Created by oldSix_Zhu on 16/8/27.
//  Copyright © 2016年 oldSix_Zhu. All rights reserved.
//

#import "ZYSTableVCHeader.h"
#import <UIImageView+WebCache.h>
#import "ZYSBtnHeaderModels.h"
#import "ZYSImgHeaderModels.h"

@interface ZYSTableVCHeader ()

//大图
@property (weak, nonatomic) IBOutlet UIImageView *image_detailView;
//帖子数
@property (weak, nonatomic) IBOutlet UILabel *post_numberLbl;
//订阅数
@property (weak, nonatomic) IBOutlet UILabel *sub_numberLbl;
//介绍
@property (weak, nonatomic) IBOutlet UILabel *infoLbl;


//按钮view
@property (weak, nonatomic) IBOutlet UIView *btnView;

//帖子
@property (weak, nonatomic) IBOutlet UILabel *postLbl;
//订阅
@property (weak, nonatomic) IBOutlet UILabel *subLbl;


//最热
@property (weak, nonatomic) IBOutlet UIButton *hotBtn;
//最新
@property (weak, nonatomic) IBOutlet UIButton *newsBtn;

//红杠
@property (nonatomic,weak)UIImageView *lineView;

@property (weak, nonatomic) IBOutlet UIView *twoBtnsView;
@end

@implementation ZYSTableVCHeader

-(void)layoutSubviews
{
//    NSLog(@"layout");
//    NSLog(@"%@",NSStringFromCGRect( self.newsBtn.frame));
    self.lineView.x = self.newsBtn.x;
}

//添加红杠
-(void)awakeFromNib
{
    UIImage *img = [UIImage imageNamed:@"friendsTrend_login_click"];
    UIImageView *line = [[UIImageView alloc]initWithImage:img];
    line.h = 2;
    line.w = ScreenW * 0.5;
    line.y = self.newsBtn.h - line.h;
    [self.twoBtnsView addSubview:line];
    self.lineView = line;
    [self didClick:self.newsBtn];
//    line.x = ScreenW * 0.5;
//    line.x = self.newsBtn.x;
//    NSLog(@"awakefram");
//    NSLog(@"%@",NSStringFromCGRect( self.newsBtn.frame));
}


-(void)setImgModel:(ZYSImgHeaderModels *)imgModel
{
    _imgModel = imgModel;
    
    [self.image_detailView sd_setImageWithURL:[NSURL URLWithString:imgModel.image_detail]];
    
    //帖子数
    self.post_numberLbl.text = imgModel.post_number;
    
    self.sub_numberLbl.text = imgModel.sub_number;
    
    self.infoLbl.text = imgModel.info;
    
}
//
-(void)setBtnArray:(NSArray *)btnArray
{
    _btnArray = btnArray;
    
    
    CGFloat lblW =  self.btnView.bounds.size.width / 6;
    CGFloat lblH = 15;
    CGFloat btnH = self.btnView.bounds.size.height - lblH - 10;
    CGFloat btnW = btnH;
    
    for (int i = 0; i < 6; i++)
    {
        ZYSBtnHeaderModels *model = btnArray[i];
        
        //添加头像
        UIImageView *btn = [[UIImageView alloc]init];
        
        //裁剪
        btn.layer.cornerRadius = btnH * 0.5;
        btn.layer.masksToBounds = YES;
        
        CGFloat btnX = lblW * i + 8;
        
        btn.frame = CGRectMake(btnX , 0, btnW, btnH);
        if (i < 5)
        {
            [btn sd_setImageWithURL:[NSURL URLWithString:model.header]];
        }
        else if (i ==5)
        {
            btn.image = [UIImage imageNamed:@"tag_user_header_more_41x41_"];
        }
       
        
        
            //添加手势
        btn.tag = i;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didClickBtn:)];
        
        [btn addGestureRecognizer:tap];
        
        btn.userInteractionEnabled = YES;
        
        [self.btnView addSubview:btn];
        
        //添加名字lbl
        UILabel *lbl = [[UILabel alloc]init];
        
        CGFloat lblX = lblW * i;
        CGFloat lblY = CGRectGetMaxY(btn.frame);
        
        lbl.frame = CGRectMake(lblX, lblY, lblW, lblH);
        
        lbl.text = model.name;
        
        lbl.font = [UIFont systemFontOfSize:11];
        
        lbl.textColor = [UIColor lightGrayColor];
        
        [self.btnView addSubview:lbl];
        
        if (i == 5)
        {
            lbl.text = @"    更多";
//            lbl.textAlignment = NSTextAlignmentCenter;
        }
       
    }
    
    
    //皇冠
    CGFloat imgW = 20;
    CGFloat imgH = imgW;
    
    for (int i = 0; i < 3; i++)
    {
        UIImageView *img = [[UIImageView alloc]init];
        
        CGFloat imgX = lblW * i;
        
        img.frame = CGRectMake(imgX, 0, imgW, imgH);
        
        NSString *imgName = [NSString stringWithFormat:@"tag_user_level_%d_27x25_",i+1];
        
        img.image = [UIImage imageNamed:imgName];
        
        [self.btnView addSubview:img];
    }
    
    
    
}

//点击事件
-(void)didClickBtn:(UITapGestureRecognizer *)tap
{
    UIImageView *btn = (UIImageView *)tap.view;
    
//    NSLog(@"点击了%ld",(long)btn.tag);
    
    //执行代理方法
    if (btn.tag == 5)
    {
        if ([self.delegate respondsToSelector:@selector(pushToBangDanTableVC:)])
        {
            [self.delegate pushToBangDanTableVC:self];
        }
    }
}

//最热
- (IBAction)didClickHotBtn:(UIButton *)sender
{
    self.newsBtn.selected = NO;
    [self didClick:sender];

}

//最新
- (IBAction)didClickNewsBtn:(UIButton *)sender
{
    self.hotBtn.selected = NO;
    [self didClick:sender];
}

//切换
-(void)didClick:(UIButton *)sender
{
    [sender setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    sender.selected = YES;
    
    long senderx = sender.x;
    
    [UIView animateWithDuration:0.4 animations:^{
        
        self.lineView.x = senderx;
//        NSLog(@"%ld",senderx);
    }];
    
    if ([self.delegate respondsToSelector:@selector(update:)])
    {
        [self.delegate update:self];
    }
}

@end
